package auth_svc

import (
	"admin-system/internal/dao"
	"admin-system/internal/models/auth_model"
	"admin-system/internal/request"
	"errors"
	"github.com/gin-gonic/gin"
)

type PermissionService struct {
	dao *dao.DaoProvider
}

func NewPermissionService(dao *dao.DaoProvider) *PermissionService {
	return &PermissionService{
		dao: dao,
	}
}

// GetPermission
// @Description: 根据角色ID获取权限列表
func (p *PermissionService) GetPermission(c *gin.Context, roleId *int64) ([]auth_model.Permission, error) {
	if roleId != nil && *roleId > 0 {
		return p.dao.PermissionDao.GetPermissionByRoleId(c, *roleId)
	}
	return p.dao.PermissionDao.GetAllPermissions(c)
}

// SavePermission
// @Description: 创建权限
func (p *PermissionService) SavePermission(ctx *gin.Context, req request.PermissionRequest) (*auth_model.
	Permission, error) {
	permission := &auth_model.Permission{
		ID:          req.ID,
		Code:        req.Code,
		Description: req.Description,
		Method:      req.Method,
		Name:        req.Name,
		Path:        req.Path,
		Type:        req.Type,
	}
	return p.dao.PermissionDao.SavePermission(ctx, permission)
}

// DelPermission
// @Description: 删除权限
func (p *PermissionService) DelPermission(ctx *gin.Context, id int64) error {
	if err := p.dao.PermissionDao.DelPermission(ctx, id); err != nil {
		return errors.New("权限删除失败")
	}
	return nil
}
